<html>
<head>
	<title>Survey</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta name="format-detection" content="telephone=no" />
	
	<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$ques_id = $_GET['qid'];
$cid = $_GET['cid'];

include 'config.php';

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

$sql = "SELECT * FROM questions WHERE QuesID = '$ques_id'";
$result = mysql_query($sql, $conn) or die(mysql_error());

echo "<form action='' method='post'>";
while ($row_ques = mysql_fetch_assoc($result)) {
	$ques_type = $row_ques['QuesType'];
	$ques = $row_ques['Question'];
	echo "$ques<br/>";
	if ($ques_type == 'Textfield') {
		echo "<input type='text' name='textfield' /><br/>";
	}
	if ($ques_type == 'Radio') {
		$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
		$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
		
		while ($row_choice = mysql_fetch_assoc($result_choice)) {
			$choice = $row_choice['choice_value'];
			$default = $row_choice['Default'];
			if ($default=='1') {
				echo "<input type='radio' name='radio' value='$choice' checked='yes' />$choice<br/>";
			} else {
				echo "<input type='radio' name='radio' value='$choice'/>$choice<br/>";
			}
		}
		echo "<br/>";
	}
	
	if ($ques_type == 'Checkbox') {
		$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
		$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
		
		while ($row_choice = mysql_fetch_assoc($result_choice)) {
			$flag = 0;
			$choice = $row_choice['choice_value'];
			$checkname = "checkbox[]";
			
			$sql_c = "SELECT Answer FROM answers WHERE CompID = '$cid' AND QuesID = '$ques_id'";
			$result_c = mysql_query($sql_c, $conn) or die(mysql_error());
			
			while ($row_c = mysql_fetch_assoc($result_c)) {
				$ans_c = $row_c['Answer'];
				if ($ans_c == $choice) {
					echo "<input type='checkbox' name='$checkname' value='$choice' checked='yes' />$choice<br/>";
					$flag = 1;
				}
			}
			
			if ($flag == 0) {
				echo "<input type='checkbox' name='$checkname' value='$choice'/>$choice<br/>";
			}
		}
		echo "<br/>";
	}
	
	if ($ques_type == 'Dropdown') {
		$sql_choice = "SELECT * FROM ques_choices WHERE QuesID='$ques_id'";
		$result_choice = mysql_query($sql_choice, $conn) or die(mysql_error());
		
		echo "<select name='dropdown'>";
		while ($row_choice = mysql_fetch_assoc($result_choice)) {
			$choice = $row_choice['choice_value'];
			echo "<option value='$choice'/>$choice</option>";
		}
		echo "</select><br/>";
	}
}
echo "<input type='submit' value='Update' name='submit'>";
echo "</form>";
?>

<?php
if (isset($_POST['submit'])) {
	putenv("TZ=US/Eastern");
	$datetime = date("Y-m-d H:i:s");
	
	$sql_del = "DELETE FROM answers WHERE CompID = '$cid' AND QuesID = '$ques_id'";
	mysql_query($sql_del, $conn) or die(mysql_error());
	
	if ($ques_type == 'Textfield') {
		$ans = $_POST['textfield'];
		$sql = "INSERT INTO answers VALUES ('$cid', '$ques_id', 'Textfield', '$datetime', '$ans')";
		mysql_query($sql, $conn) or die(mysql_error());
	}
	
	if ($ques_type == 'Radio') {
		$ans = $_POST['radio'];
		$sql = "INSERT INTO answers VALUES ('$cid', '$ques_id', 'Radio', '$datetime', '$ans')";
		mysql_query($sql, $conn) or die(mysql_error());
	}
	
	if ($ques_type == 'Checkbox') {
		$ans_array = $_POST['checkbox'];
		$n = count($ans_array);
		for ($i = 0; $i < $n; $i++) {
			$ans = $ans_array[$i];
			$sql = "INSERT INTO answers VALUES ('$cid', '$ques_id', 'Checkbox', '$datetime', '$ans')";
			mysql_query($sql, $conn) or die(mysql_error());
		}
	}
	
	if ($ques_type == 'Dropdown') {
		$ans = $_POST['dropdown'];
		$sql = "INSERT INTO answers VALUES ('$cid', '$ques_id', 'Dropdown', '$datetime', '$ans')";
		mysql_query($sql, $conn) or die(mysql_error());
	}
	
	header ("location: showsurvey.php?comp_id=$cid");
}
?>
</body>
</html>